(function(window){

    var ShopCart=function(prefix,defCart){
        Find.prototype.prefix=prefix;
        cart = new Cart(document.getElementsByClassName(prefix)[0]);
        for(var i in defCart){
            cart.add(defCart[i]);
        }
        cart.updateTotal();
    };
    function Find(obj){
        this.obj=obj;
    }
    Find.prototype.prefix='';
    Find.prototype.className=function(className){
        return this.obj.getElementsByClassName(this.prefix + '-' + className)[0];
    };
    function Cart(obj) {
        this.items=[];
        var find =new Find(obj);
        this.all = find.className('all');
        this.bottom = find.className('bottom');
        this.num = find.className('total-num');
        this.price = find.className('total-price');
        this.tmp = find.className('item');
        this.tmp.parentNode.removeChild(this.tmp);
        var cart = this;
        this.all.onclick= function(){
            cart.checkAll();
        };
    }
    Cart.prototype.add = function(data){
        for (let i = 0; i < this.items.length; i++) {
            if (this.items[i].name.textContent == data.name) {
                this.items[i].addFun();
                return;
            }
        }
        var tmp = this.tmp.cloneNode(true);
        var item = new Item(tmp, data);
        item.updateSubtotal();
        this.items.push(item);
        this.bottom.before(tmp);
        var cart = this;
        cart.updateTotal();
        item.check.onclick = function (){
            cart.updateTotal();
        };
        item.addFun = addFun;
        item.add.onclick =addFun;

        function addFun(){
            item.num.textContent = ++item.data.num;
            item.updateSubtotal()
            cart.updateTotal();
        };

        function addTotal() {
            item.updateSubtotal()
            cart.updateTotal();
        }
        item.reduce.onclick = function() {
            if(item.data.num>1){
                item.num.textContent = --item.data.num;
                item.updateSubtotal();
                cart.updateTotal();
            }else{
                alert('至少选择1件，如果不需要，请直接删除');
            }
        };
        item.del.onclick = function(){
            if (confirm('您确定要删除此商品吗？')){
                tmp.parentNode.removeChild(tmp);
                cart.del(item);
                cart.updateTotal();
            }
        };
    };
    Cart.prototype.updateTotal = function(){
        var num = 0, price = 0;
        for(var i in this.items){
            var item = this.items[i];
            if(item.check.checked){
                num += item.data.num;
                price += item.data.num * item.data.price;
            }
        }
        this.num.textContent = num;
        this.price.value = price.toFixed(2);
    };
    Cart.prototype.checkAll = function(){
        for(var i in this.items){
            this.items[i].check.checked = true;
        }
        this.updateSubtotal();
    }
    Cart.prototype.del = function(item){
        for(var i in this.items){
            if(this.items[i] === item){
                // delete this.items[i];
                // this.items[i] = null;
                this.items = _.without(this.items,this.items[i]);
                cart.updateTotal();
                break;
            }
        }
    }

    function Item(tmp,data){
        var find = new Find(tmp);
        this.check = find.className('check');
        this.name = find.className('name');
        this.price = find.className('price');
        this.num = find.className('num');
        this.add = find.className('add');
        this.reduce = find.className('reduce');
        this.subtotal = find.className('subtotal');
        this.del = find.className('del');
        this.data = data;
        this.name.textContent = data.name;
        this.price.textContent = data.price.toFixed(2);
        this.num.textContent = data.num;
    }
    Item.prototype.updateSubtotal = function(){
        this.subtotal.textContent = (this.data.num*this.data.price).toFixed(2);
    };
    window['ShopCart'] = ShopCart;
})(window);

window.onload = function () {
    var cart;
    ShopCart('cart',[
    ]);
}




